package system

import (
	"gin-admin/common/dto/response"
	"gin-admin/core"
	"gin-admin/service/dto/request"
	"gin-admin/service/v2/system"

	"github.com/gin-gonic/gin"
)

type CaptchaHandler struct {
	CaptchaService system.CaptchaService
}

func NewCaptchaHandlerApp(service *system.CaptchaService) *CaptchaHandler {
	return &CaptchaHandler{
		CaptchaService: *service,
	}
}

// @Summary 获取验证码(v2)
// @Tags /v2/验证码管理
// @Accept json
// @Product json
// @Success 200 {object} response.Response{data=response.CaptchaResponse} "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/captcha [get]
func (ch *CaptchaHandler) GetCaptchaHandler(ctx *gin.Context) {
	result, err := ch.CaptchaService.GetCaptchaService()
	if err != nil {
		response.FailedWithCode(ctx, response.GetCaptchaFailedCode)
		return
	}
	response.Success(ctx, result)
}

// @Summary 验证验证码(v2)
// @Tags /v2/验证码管理
// @Accept json
// @Product json
// @Param data body request.VerifyCaptchaRequest true "验证验证码请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/captcha/verify [post]
func (ch *CaptchaHandler) VerifyCaptchaHandler(ctx *gin.Context) {
	var captchaRequest request.VerifyCaptchaRequest
	if !core.ValidateParams(ctx, &captchaRequest) {
		return
	}
	result, err := ch.CaptchaService.VerifyCaptchaService(&captchaRequest)
	if err != nil {
		response.FailedWithCode(ctx, response.CaptchaInvalidCode)
		return
	}
	response.Success(ctx, result)
}
